package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhao
 * @Date: 2020/12/3 10:21
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerDao customerDao;

    /**
     * 客户列表分页（名称模糊查询）
     *
     * @param page
     * @param rows
     * @return
     */
    @Override
    public List<Customer> getCustomer(Integer page, Integer rows, String customerName) {

        PageHelper.startPage(page, rows);
        List<Customer> customerList = new ArrayList<>();

        if (!StringUtils.isEmpty(customerName)) {
            //如果查询条件不为null，进行条件查询
            customerList = customerDao.getLikeCustomer("%" + customerName + "%");
        } else {
            customerList = customerDao.getCustomer();
        }

        return customerList;
    }

    /**
     * 客户添加或修改
     *
     * @param customer
     * @return
     */
    @Override
    public ServiceVO addCustomer(Customer customer) {

        if (customer.getCustomerId() == null) {
            //id为null，添加操作
            customerDao.addCustomer(customer);
        }else {
            //修改操作
            customerDao.updateCustomer(customer);
        }

        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS,null);
    }


    /**
     * 客户删除（支持批量删除）
     * @param ids
     * @return
     */
    @Override
    public ServiceVO deleteCustomer(String ids) {

        String[] split = ids.split(",");
        for (String s : split){
            customerDao.deleteCustomer(Integer.valueOf(s).intValue());
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS,null);
    }
}
